<template>
  <div class="app-container">
    <el-card class="common-card query-box">
      <div class="queryForm">
        <el-form :model="queryParams" ref="queryRef" :inline="true"
                 @submit.native.prevent>
          <el-form-item label="账套名称">
            <el-input
                v-model="queryParams.name"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">{{ t('org.button.query') }}</el-button>
            <el-button @click="resetQuery">{{ t('org.button.reset') }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="common-card">
      <div class="btn-form">
        <el-button
            type="primary"
            @click="handleAdd"
        >{{ t('org.button.add') }}
        </el-button>
        <el-button
            type="danger"
            :disabled="ids.length === 0"
            @click="onBatchDelete"
        >{{ t('org.button.deleteBatch') }}
        </el-button>
      </div>
      <el-table
          v-loading="loading"
          :data="setsList"
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
         <el-table-column prop="id" label="编码" align="center" min-width="80"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="name" label="名称" align="left" min-width="100"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="companyName" label="单位名称" align="left" min-width="100"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="vatType" label="纳税性质" align="center" min-width="70">
          <template #default="scope">
            <dict-tag-number :options="books_vat_type" :value="scope.row.vatType"/>
          </template>
        </el-table-column>
        <el-table-column prop="standardsName" label="会计准则" align="left" min-width="80"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="enableDate" label="建账期间" align="center" min-width="40"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="voucherReviewed" label="凭证审核" align="center" min-width="40">
          <template #default="scope">
            <span v-if="scope.row.voucherReviewed === 0">关闭</span>
            <span v-if="scope.row.voucherReviewed === 1">开启</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" :label="t('org.status')" align="center" min-width="40">
          <template #default="scope">
                <span v-if="scope.row.status === 1"><el-icon color="green"><SuccessFilled
                    class="success"/></el-icon></span>
            <span v-if="scope.row.status === 0"><el-icon color="#808080"><CircleCloseFilled/></el-icon></span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('jbx.text.action')" align="center" width="120">
          <template #default="scope">
            <el-tooltip content="编辑">
              <el-button link icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="关联会计科目">
              <el-button plain type="text" @click="showSubjects(scope.row)">
                <svg-icon icon-class="menus-huijikemu" size="0.9em"></svg-icon>
              </el-button>
            </el-tooltip>
            <el-tooltip content="移除">
              <el-button link icon="Delete" type="danger" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNumber"
          v-model:limit="queryParams.pageSize"
          :page-sizes="queryParams.pageSizeOptions"
          @pagination="getList"
      />
    </el-card>
    <edit-form :title="title" :open="open"
               :form-id="id"
               :accounting_standards="standardList"
               :vat_types="books_vat_type"
               :books_industry="books_industry"
               @dialogOfClosedMethods="dialogOfClosedMethods"></edit-form>
    <!--会计科目-->
    <subjects-related :title="title" :form-id="id" :open="subjectOpen" @hatsDrawerClose="dialogOfClosedMethods"/>
  </div>
</template>

<script setup lang="ts">
import {useI18n} from "vue-i18n";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import editForm from "./edit.vue";
import subjectsRelated from "./subject-relate.vue";
import modal from "@/plugins/modal";
import DictTagNumber from "@/components/DIctTagNumber/index.vue";
import {listBooksSets, deleteBatch} from "@/api/system/book/book";
import {listStandardsAll} from "@/api/system/standard/standard";
import SvgIcon from "@/components/SvgIcon/index.vue";

const {t} = useI18n()

const {proxy} = getCurrentInstance()!;

const {books_vat_type, books_industry}
    = proxy?.useDict("books_vat_type", "books_industry");

const data: any = reactive({
  queryParams: {
    pageNumber: 1,
    pageSize: 10,
    pageSizeOptions: [10, 20, 50]
  }
});

const {queryParams} = toRefs(data);

const setsList: any = ref<any>([]);
const open: any = ref(false);
const subjectOpen: any = ref(false);
const loading: any = ref(true);
const title: any = ref("");
const id: any = ref(undefined);
const total: any = ref(0);
const ids: any = ref<any>([]);
const selectionlist: any = ref<any>([]);
//会计准则
const standardList: any = ref<any>([]);


/**
 * 获取列表
 */
function getList(): any {
  listBooksSets(queryParams.value).then((res: any) => {
    if (res.code === 0) {
      loading.value = false;
      setsList.value = res.data.records;
      total.value = res.data.total;
    }
  })
}

/** 多选操作*/
function handleSelectionChange(selection: any): any {
  selectionlist.value = selection;
  ids.value = selectionlist.value.map((item: any) => item.id);
}

/**
 * 查询
 */
function handleQuery(): any {
  queryParams.value.pageNumber = 1;
  getList();
}

/**
 * 重置
 */
function resetQuery(): any {
  queryParams.value.code = undefined;
  queryParams.value.name = undefined;
  queryParams.value.category = undefined;
  handleQuery();
}

function handleAdd(): any {
  id.value = undefined;
  title.value = t('jbx.text.add')
  open.value = true;
}

/*关闭抽屉*/
function dialogOfClosedMethods(val: any): any {
  open.value = false;
  subjectOpen.value = false;
  id.value = undefined;
  if (val) {
    getList();
  }
}


/** 多选删除操作*/
function onBatchDelete(): any {
  modal.confirm(t('jbx.confirm.text.delete')).then(function () {
    return deleteBatch({listIds: ids.value});
  }).then((res: any) => {
    if (res.code === 0) {
      handleQuery();
      modal.msgSuccess(t('jbx.alert.delete.success'));
    } else {
      modal.msgError(res.message);
    }
  }).catch(() => {
  });
}

interface TreeNode {
  id: string | number; // 根据你的数据实际情况调整类型
  children?: TreeNode[]; // 子节点可能不存在
}

/** 修改按钮操作 */
function handleUpdate(row: any): any {
  id.value = row.id;
  title.value = t('org.titleEdit');
  open.value = true;
}

/** 删除按钮操作 */
function handleDelete(row: any): any {
  modal.confirm(t('org.deleteTip1') + row.name + t('org.deleteTip2')).then(function () {
    return deleteBatch({listIds: [row.id]});
  }).then((res: any) => {
    if (res.code === 0) {
      getList();
      modal.msgSuccess(t('jbx.alert.delete.success'));
    } else {
      modal.msgError(res.message);
    }
  }).catch(() => {
  });
}

function getStandards(): any {
  listStandardsAll({status: 1}).then((res: any) => {
    if (res.code === 0) {
      standardList.value = res.data;
    }
  });
}

/*查看关联的会计科目*/
function showSubjects(row: any): any {
  id.value = row.id;
  title.value = '已关联的会计科目'
  subjectOpen.value = true;
}

getStandards();
getList();
</script>

<style lang="scss" scoped>
.btn-form {
  margin-bottom: 10px;
}

.common-card {
  margin-bottom: 15px;
}

.app-container {
  padding: 0;
  background-color: #f5f7fa;
}
</style>
